//
//  KRRoundView.m
//  KEREN
//
//  Created by MW on 2023/4/23.
//

#import "KRRoundView.h"
#import "KRFindCarouselModel.h"

@implementation KRRoundView
- (instancetype)initWithFrame:(CGRect)frame imgArray:(NSArray *)array{
    self = [super initWithFrame:frame];
    return self;
}

- (void)setImageView:(NSArray *)array{
    self.imgArray=[[NSMutableArray alloc]init];
    [self.imgArray addObject:[array lastObject]];
    [self.imgArray addObjectsFromArray:array];
    [self.imgArray addObject:[array firstObject]];
    for(int i = 0; i < self.imgArray.count; i++){
        CGRect imgFrame = CGRectMake(self.frame.size.width* i, 0, self.frame.size.width , self.frame.size.height);
        UIImageView *imgView=[[UIImageView alloc]initWithFrame:imgFrame];
        KRFindCarouselModel *model = self.imgArray[i];
        [imgView sd_setImageWithURL:[NSURL URLWithString:model.image_url] placeholderImage:[UIImage imageNamed:@"find_big_icon"]];
        [self.scrollView addSubview:imgView];
//        UILabel *titleLbl = [[UILabel alloc] init];
//        [imgView addSubview:titleLbl];
//        [titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.offset(24);
//            make.right.offset(-24);
//            make.centerY.equalTo(imgView);
//        }];
//        titleLbl.textAlignment = NSTextAlignmentCenter;
//        titleLbl.font = BFont(28);
//        titleLbl.textColor = Whire_Color;
//        titleLbl.text = model.name;
    }
    self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, self.frame.size.height-20, self.frame.size.width, 20)];
    self.pageControl.numberOfPages = array.count;
    self.pageControl.currentPage = 0;
    self.pageControl.tintColor = [UIColor whiteColor];
    self.timer=[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(scrollImage) userInfo:nil repeats:YES];
    //添加到runloop中
    NSRunLoop *runloop = [NSRunLoop currentRunLoop];
    [runloop addTimer:self.timer forMode:NSRunLoopCommonModes];
    
    [self addSubview:self.scrollView];
    [self addSubview:self.pageControl];
}

- (UIScrollView *)scrollView{
    if(!_scrollView){

        self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];

        self.scrollView.contentSize= CGSizeMake(self.frame.size.width * self.imgArray.count, self.frame.size.height);
        self.scrollView.pagingEnabled = YES;
        self.scrollView.contentOffset = CGPointMake(self.frame.size.width, 0);
        self.scrollView.showsVerticalScrollIndicator = NO;
        self.scrollView.showsHorizontalScrollIndicator = NO;
        self.scrollView.delegate = self;
        [self.scrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
    }
    return _scrollView;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offsetX = scrollView.contentOffset.x;
    offsetX += scrollView.frame.size.width*0.5;
     
    //因为最前面还有一个imgView
    int page = offsetX/scrollView.frame.size.width-1;
    self.pageControl.currentPage = page;
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.timer invalidate];
    self.timer = nil;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(scrollImage) userInfo:nil repeats:YES];
     
    //优先级 设置到当前的runloop中
    NSRunLoop *runloop = [NSRunLoop currentRunLoop];
    [runloop addTimer:self.timer forMode:NSRunLoopCommonModes];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
     
    if([keyPath isEqualToString:@"contentOffset"]){
        //[change objectForKey:NSKeyValueChangeNewKey] 是一个对象
        CGPoint offset= [[change objectForKey:NSKeyValueChangeNewKey] CGPointValue];
        //当划到3后面的1时
        if(offset.x >= self.scrollView.contentSize.width-self.scrollView.frame.size.width){
            [self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width, 0)];
            self.pageControl.currentPage=0;
        }
        //当划到1前面的3时
        else if(offset.x <= 0){
            [self.scrollView setContentOffset:CGPointMake(self.scrollView.contentSize.width-2*self.scrollView.frame.size.width, 0)];
            self.pageControl.currentPage=self.pageControl.numberOfPages-1;
        }
    }
}
-(void)scrollImage{
    NSInteger page = [self.pageControl currentPage];
    page++;
    CGFloat offsetX = page * self.scrollView.frame.size.width+self.scrollView.frame.size.width;
    [self.scrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
}
@end
